#!/usr/bin/python3
# Filename : UpdateDigkey.py
#Author:Bill Zhang
#Email:zchv@msn.com
#Digkey collection thread

import urllib2
#import the BeautifulSoup which parse the html
from bs4 import BeautifulSoup
from bs4.diagnose import diagnose
#import python's reguler expression lib
import re

def getData(URL):
	#URL='http://www.digikey.cn/product-detail/zh/BFC280800006/BC2767-ND/3028995'
	opener = urllib2.build_opener()
	opener.addheaders = [('User-agent', 'Mozilla/5.0')]
	#HtmlCode save the source code of the webpages
	HtmlCode=opener.open(URL).read().decode('utf-8')
	if HtmlCode!=0:
	    print("having got the html source")
	##    print(HtmlCode)
	##parse the html by using the BeautifulSoup

	##f = open(URL[-7:], 'w') # open for 'w'riting
	##f.write(HtmlCode) # write text to file
	##f.close() # close the file

	#re extract content digkey pattern
	Digkey=BeautifulSoup(HtmlCode,"lxml")

	#get product's quantity 
	quantity=Digkey.find(id="quantityavailable").text
	ProductQuantity=re.search(r"\d*,\d*",quantity).group(0).replace(',','')
	# print(ProductQuantity)

	#get product's price
	price=Digkey.find(id="pricing")
	price=str(price).replace(',','')

	ProductPrice=re.findall(r"(?x)(\d*[.0-9]\d*)</td>",price)
	
	ProductPrice.insert(0,ProductQuantity)
	
	return ProductPrice
